﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Home.Utils;
using NhanAi.DAO;
using NhanAi.DAO.Mix;
using NhanAi.Entity;
using NhanAi.App_Code;
namespace NhanAi.Admin
{
    public partial class CreateCPost : BasePage
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            Authenticate();  
            cmbStatus.Items.Add(new ListItem("Nháp", NAConstants.POST_STA_DRAFT.ToString()));
            cmbStatus.Items.Add(new ListItem("Cho phép đăng", NAConstants.POST_STA_POST.ToString()));
         
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            PostDao dao = new PostDao();
            Post p = new Post();
            bool rs = false;
            try
            {
                
                p.Title = txtTitle.Text;
                p.TitleEn ="";
                p.Content = PostContent.Value;
                p.ContentEn = "";
                p.Status = int.Parse(cmbStatus.SelectedItem.Value);
                p.PostAttribute = 0;
                p.PostDate = DateTime.Now;
                p.ModifiedDate = DateTime.Now;
                p.MenuOrder = 0;
                p.Protected = false;
                p.Guid ="";
              
                p.PostType = "cp";
                p.Excerpt = StringUltility.GetExcerpt(p.Content,NAConstants.EXCERPT_LEN);
                p.ExcerptEn = "";
                p.Name = "";

                if (Session["user"] != null)
                {
                    User u = (User)Session["user"];
                    p.AuthorId = u.Id;
                }
             
                dao.Create(p);
                rs = true;
                SetMessage("Thêm mới thành công", "");
            }
            catch (Exception ex)
            {
                SetMessage("Đã xảy ra lỗi","");
            }
          
            if(rs)Redirect("/Admin/EditCPost.aspx?id=" + p.Id);
        }
       
    }
}